package org.sheep.sprite;

import org.sheep.R;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public abstract class Sprite {
  protected final static int MAX_LEVEL = 4;
  protected final static int GAP_TO_OPPONENT = 3; //pixels
  
  protected int position;
  protected int length;
  
  protected int[] strength = new int[MAX_LEVEL];
  protected int[] lifes = new int[MAX_LEVEL];
  
  protected int level = 0;
  protected boolean alive = true;
  protected boolean inFight;
  
  protected Bitmap image;
  
  private int xOffset = 0;
  private int yOffset = 0;
  
  public void attack(Sprite s) {
    lifes[level] -= strength[level];
    if(lifes[level] < 0){
      alive = false;
    }
  }
  
  public void setInFight(boolean b) {
    inFight = b;
  }
  
  public boolean getInFight() {
    return inFight;
  }
  
  public void setPosition(int pos) {
    position = pos;
  }
  
  public int getPosition(){
    return position;
  }
  
  public boolean isAlive() {
    return alive;
  }
  
  public boolean inFrontOf(Sprite s){
    return (position + length) + GAP_TO_OPPONENT >= s.position;
  }

  public void setSize(int width, int height, int viewWidth, int viewHeight, Context context) {
    int xTileSize = (width/R.attr.width) - xOffset;
    int yTileSize = (height/R.attr.height) - yOffset;
    Resources res = context.getResources();
    image = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res,
            getResourceId()), xTileSize, yTileSize, true); 
  }
  
  protected abstract int getResourceId();
  
  public Bitmap getImage() {
    return image;
  }
}
